<template>
  <div class="loginPage">
    <el-card class="box-card">
      <div class="box-card-top">
        <!-- <img src="~@/assets/imgs/personal_logo.jpg" style="width: 150px" /> -->
        <el-select
          v-model="data.lang"
          placeholder="Select"
          size="large"
          @change="langChange"
        >
          <el-option
            v-for="item in langOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </div>
      <el-form
        class="form-control"
        :model="formData"
        :rules="loginRules"
        ref="formRef"
      >
        <el-form-item prop="userName">
          <el-input
            v-model="formData.userName"
            :placeholder="$t('message.login.userName')"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formData.password"
            type="password"
            :placeholder="$t('message.login.password')"
          />
        </el-form-item>
        <el-form-item>
          <div
            style="
              width: 100%;
              display: flex;
              justify-content: flex-end;
              align-items: center;
            "
          >
            <el-button
              type="primary"
              @click="submitLogin(formRef)"
              :loading="data.loginLoading"
            >
              {{ $t("message.login.login") }}
            </el-button>
            <el-button @click="reset(formRef)">{{
              $t("message.login.reset")
            }}</el-button>
          </div>
        </el-form-item>
      </el-form>
      <div style="text-align: center">
        <img src="~@/assets/imgs/personal_logo.jpg" style="width: 150px" />
      </div>
    </el-card>
  </div>
</template>
<script setup>
import { reactive, toRefs, ref, onActivated, nextTick, toRaw } from "vue";
import router from "@/router";
import { useUserStore } from "@/stores/userStore";
import { usePermissionStore } from "@/stores/permissionStore";
import { ruleString } from "@/utils/rules/rule";
import apis from "@/api/index";
import { searchRoute } from "@/utils/util";

onActivated(async () => {
  await nextTick();
  // 进入页面重置表单
  formRef.value && formRef.value.resetFields();
  // 判断是否有token, 进入页面
  const token = window.localStorage.getItem("accessToken");
  if (token) {
    const lastRoute = window.localStorage.getItem("lastRoute");
    router.push(lastRoute);
  }
});

const formRef = ref(null);
const user = useUserStore();
const permissionStore = usePermissionStore();
const data = reactive({
  formData: {
    userName: "",
    password: "",
  },
  loginRules: {
    userName: ruleString(),
    password: ruleString(),
  },
  langOptions: [
    { value: "zh_CN", label: "简体中文" },
    { value: "en_US", label: "EN" },
  ],
  lang: user.user.lang,
  loginLoading: false,
});
const { formData, loginRules, langOptions } = toRefs(data);

function langChange(value) {
  user.setLanguage(value);
}

async function submitLogin(formEl) {
  if (!formEl) {
    return;
  }
  data.loginLoading = true;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      const { formData: requestFormData } = toRaw(data);
      const loginRes = await apis.auth
        .login({
          ...requestFormData,
        })
        .catch((err) => {
          console.log(err);
        });
      if (loginRes?.data) {
        user.setUserInfo(loginRes.data.userInfo);
        window.localStorage.setItem("accessToken", loginRes.data.token);
        await permissionStore.setMenuListAction();
        const lastRoute = window.localStorage.getItem("lastRoute");
        if (permissionStore.permission.menuList?.length) {
          if (
            lastRoute &&
            searchRoute(lastRoute, permissionStore.permission.menuList).path
          ) {
            router.push(lastRoute);
          } else {
            router.push(permissionStore.permission.menuList[0].path);
          }
        }
      }
    } else {
      console.log("error login!", fields);
    }
    data.loginLoading = false;
  });
}

function reset(formEl) {
  formEl && formEl.resetFields();
}
</script>
<style lang="scss" scoped>
.loginPage {
  width: 100%;
  height: 100%;
  @include bg_color("bg_color");
  position: relative;
  .box-card {
    min-width: 400px;
    max-height: 100%;
    overflow: auto;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .box-card-top {
      display: flex;
      justify-content: flex-end;
      // align-items: center;
      .el-select {
        width: 150px;
      }
    }
    .form-control {
      padding: 20px 0px;
    }
  }
  .el-button--primary {
    @include bg_color("bg_color");
    &:hover {
      @include hover_bg_color("hover_bg_color");
    }
  }
}
</style>
